$(function () {
    $('.btn-reset').click(function () {
        $(this).parent('form').find('input,select').val('');
    });
    // 全选的实现
    $(".check-all").click(function () {
        $(".ids").prop("checked", this.checked);
    });
    $(".ids").click(function () {
        var option = $(".ids");
        option.each(function (i) {
            if (!this.checked) {
                $(".check-all").prop("checked", false);
                return false;
            } else {
                $(".check-all").prop("checked", true);
            }
        });
    });


    // ajax get请求
    $('.ajax-get').click(function () {
        var target;
        var that = this;
        if ($(this).hasClass('confirm')) {
            if (!confirm('确认要执行该操作吗?')) {
                return false;
            }
        }
        if ((target = $(this).attr('href')) || (target = $(this).attr('data-url'))) {
            $.get(target).success(function (data) {
                var info = data.msg;
                if (data.code == 1) {
                    var msg = new $.zui.Messager(info, {
                        placement: 'top',
                        type: 'success'
                    });
                    msg.show();
                    setTimeout(function () {
                            $(that).removeClass('disabled').prop('disabled', false);
                            if (data.url) {
                                location.href = data.url;
                            } else {
                                location.reload();
                            }
                        },
                        2000);
                } else {
                    var info = data.msg;
                    if (typeof(info) == 'string') {
                        var msg = new $.zui.Messager(info, {
                            placement: 'top',
                            type: 'danger'
                        });
                        msg.show();
                    } else {
                        for (var item in info) {
                            var element = form.find("input[name='" + item + "']");
                            element.next('.help-block').remove();
                            element.after('<span class="help-block text-error">' + info[item] + '</span>');
                            element.parents('div.form-group').addClass('has-error');
                        }
                        var msg = new $.zui.Messager('数据提交异常', {
                            placement: 'top',
                            type: 'success'
                        });
                        msg.show();
                    }

                    setTimeout(function () {
                            $(that).removeClass('disabled').prop('disabled', false);
                        },
                        3000);
                }
            });

        }
        return false;
    });

    // ajax post submit请求
    $('body').on('click', '.ajax-post',
        function () {
            var target, query, form;
            var target_form = $(this).attr('data-href');
            var that = this;
            var nead_confirm = false;

            if (($(this).attr('type') == 'submit') || (target = $(this).attr('href')) || (target = $(this).attr('url'))) {
                form = $('.' + target_form);

                if ($(this).attr('data-hide') === 'true') { // 无数据时也可以使用的功能
                    form = $('.data-hide');
                    query = form.serialize();
                } else if (form.get(0) == undefined) {

                    return false;
                } else if (form.get(0).nodeName == 'FORM') {
                    if ($(this).hasClass('confirm')) {
                        if (!confirm('确认要执行该操作吗?')) {
                            return false;
                        }
                    }
                    if ($(this).attr('url') !== undefined) {
                        target = $(this).attr('url');
                    } else {
                        target = form.attr('action');
                    }
                    query = form.serialize();
                } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                    form.each(function (k, v) {
                        if (v.type == 'checkbox' && v.checked == true) {
                            nead_confirm = true;
                        }
                    });
                    if (nead_confirm && $(this).hasClass('confirm')) {
                        if (!confirm('确认要执行该操作吗?')) {
                            return false;
                        }
                    }
                    query = form.serialize(true);
                } else {

                    if ($(this).hasClass('confirm')) {
                        if (!confirm('确认要执行该操作吗?')) {
                            return false;
                        }
                    }
                    query = form.find('input,select,textarea').serialize();
                }

                $(that).addClass('disabled').attr('autocomplete', 'off').prop('disabled', true);

                $.post(target, query).success(function (data) {
                    var info = data.msg;
                    if (data.code == 1) {
                        var msg = new $.zui.Messager(info, {
                            placement: 'top',
                            type: 'success'
                        });
                        msg.show();
                        setTimeout(function () {
                                $(that).removeClass('disabled').prop('disabled', false);
                                if (data.url) {
                                    location.href = data.url;
                                } else {
                                    location.reload();
                                }
                            },
                            2000);
                    } else {
                        if (typeof(info) == 'string') {
                            var msg = new $.zui.Messager(info, {
                                placement: 'top',
                                type: 'danger'
                            });
                            msg.show();
                        } else {
                            for (var item in info) {
                                var element = form.find("input[name='" + item + "']");
                                element.next('.help-block').remove();
                                element.after('<span class="help-block text-error">' + info[item] + '</span>');
                                element.parents('div.form-group').addClass('has-error');
                            }
                            var msg = new $.zui.Messager('数据提交异常', {
                                placement: 'top',
                                type: 'danger'
                            });
                            msg.show();

                        }

                        setTimeout(function () {
                                $(that).removeClass('disabled').prop('disabled', false);
                            },
                            3000);
                    }
                });
            }
            return false;
        });
});